<?php
require_once dirname(__FILE__) . "/../config/functions.php";
include_once dirname(__FILE__) . '/header.php';

if (isset($_POST) && $_POST) {
    foreach ($_POST as $key => $value) {
        //check is exist
        $check = mysql_query("SELECT * FROM `option` where `key` = '$key' ");
        if (mysql_num_rows($check) > 0) {
            mysql_query("update `option` set `value` = '$value' where `key` = '$key' ");
        } else {
            mysql_query("insert into `option` (`value`, `key`) values ('$value', '$key') ");
        }
    }
}

//get all options
$options = mysql_query("SELECT `value` FROM `option` where `key` = '" . $_GET['page'] . "'");
$pageContent = '';
while ($option = mysql_fetch_array($options)) {
    $pageContent = $option['value'];
}
?>
<div id="page-wrapper">

    <div class="row">
        <div class="col-lg-12">
            <h1>Edit <small>Page</small></h1>
            <?php if (isset($_POST) && $_POST) : ?>
                <div class="alert alert-success alert-dismissable">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                    Settings updated successfully.
                </div>
            <?php endif; ?>

            <form class="form-horizontal" role="form" method="post">
                <div class="form-group">
                    <label for="<?php echo $_GET['page']; ?>" class="col-sm-2 control-label">Page Content</label>
                    <div class="col-sm-10">
                        <textarea class="form-control" rows="10" name="<?php echo $_GET['page']; ?>" id="page_content"><?php echo $pageContent; ?></textarea>
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-primary">Update</button>
                    </div>
                </div>
            </form>

        </div>
    </div><!-- /.row -->

</div><!-- /#page-wrapper -->
<?php include_once dirname(__FILE__) . '/footer.php'; ?>

